package com.iman.manjusri.util;

import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import com.iman.common.support.Page;
import com.iman.common.util.CollectionUtils;

public abstract class PageUtil {
    public static final String PAGE_SIZE    = "pageSize";
    public static final String CURRENT_PAGE = "currentPage";

    public static final Map newPageParams(int currentPage,int pageSize){
        return CollectionUtils.newMap(PageUtil.CURRENT_PAGE, currentPage, PageUtil.PAGE_SIZE, pageSize);
    }
    
    public static final Map newPageParams(Integer currentPage,Integer pageSize){
        if(null == currentPage){
            currentPage = 1;
        }
        if(null == pageSize){
            pageSize = Page.DEFAULT_PAGE_SIZE;
        }
        return CollectionUtils.newMap(PageUtil.CURRENT_PAGE, currentPage, PageUtil.PAGE_SIZE, pageSize);
    }
    
    public static final Map newPageParams(){
        return CollectionUtils.newMap(PageUtil.CURRENT_PAGE, 1, PageUtil.PAGE_SIZE, Page.DEFAULT_PAGE_SIZE);
    }
    
    //pageNo is 1-based
    public static final int getCurrentPageNo(int pageNo) {
        if (0 == pageNo) {
            return 1;
        } else {
            return pageNo;
        }
    }

    public static final int getCurrentPageNo(String pageNo) {
        Integer pageNoInt = NumberUtils.createInteger(pageNo);
        return getCurrentPageNo(pageNoInt);
    }

    public static final int getCurrentPageNo(Integer pageNo) {
        if (null == pageNo) {
            return 1;
        } else {
            return getCurrentPageNo(pageNo.intValue());
        }
    }
    
    public static final int getCurrentPageNo(Map params) {
        if(null == params){
            return 1;
        }
        Integer currentPage = (Integer) params.get(CURRENT_PAGE);
        return getCurrentPageNo(currentPage);
    }

    public static final int getPageSize(int pageSize) {
        if (0 == pageSize) {
            return Page.DEFAULT_PAGE_SIZE;
        } else {
            return pageSize;
        }
    }

    public static final int getPageSize(String pageSize) {
        Integer pageSizeInt = NumberUtils.createInteger(pageSize);
        return getPageSize(pageSizeInt);
    }

    public static final int getPageSize(Integer pageSize) {
        if (null == pageSize) {
            return Page.DEFAULT_PAGE_SIZE;
        } else {
            return getPageSize(pageSize.intValue());
        }
    }
    
    public static final int getPageSize(Map params) {
        if(null == params){
            return Page.DEFAULT_PAGE_SIZE;
        }
        Integer pageSize = (Integer) params.get(PAGE_SIZE);
        return getPageSize(pageSize);
    }
}
